package com.sky.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.DishService;
import com.sky.service.SetmealService;
import com.sky.service.ShoppingCartService;
import com.sky.vo.DishVO;
import com.sky.vo.SetmealVO;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
@AllArgsConstructor
public class ShoppingCartServiceImpl extends ServiceImpl<ShoppingCartMapper,ShoppingCart> implements ShoppingCartService {

    private final ShoppingCartMapper shoppingCartMapper;
    private final DishService dishService;
    private final SetmealService setmealService;


    // 添加购物车
    @Override
    @Transactional
    public void add(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        Long userId = BaseContext.getCurrentId();
        Long dishId = shoppingCartDTO.getDishId();
        // 判断购物车里是否添加了相同的菜品或套餐
        ShoppingCart dishCart = shoppingCartMapper.selectShoppingCart(shoppingCartDTO,userId);
        // 如果不为空，说明添加了相同的菜品或套餐,修改其数量和金额
        if(dishCart != null){
            BeanUtils.copyProperties(dishCart,shoppingCart);
            BigDecimal amount = dishCart.getAmount(); // 总价
            BigDecimal number = BigDecimal.valueOf(dishCart.getNumber()); // 数量
            BigDecimal price = amount.divide(number); // 单价
            shoppingCartMapper.updateShoppingCart(shoppingCartDTO,price);
        }else {
            // 若为空，则添加菜品或套餐
            shoppingCart.setUserId(userId);
            shoppingCart.setDishFlavor(shoppingCartDTO.getDishFlavor());
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            // 如果菜品id不为null，则说明添加的是菜品
            if(shoppingCartDTO.getDishId() != null){
                DishVO dishVO = dishService.selectById(dishId);
                shoppingCart.setName(dishVO.getName());
                shoppingCart.setAmount(dishVO.getPrice());
                shoppingCart.setImage(dishVO.getImage());
                shoppingCart.setDishId(dishId);
            }else {
                // 否则，添加的是套餐
                SetmealVO setmealVO = setmealService.selectById(shoppingCartDTO.getSetmealId());
                shoppingCart.setName(setmealVO.getName());
                shoppingCart.setAmount(setmealVO.getPrice());
                shoppingCart.setImage(setmealVO.getImage());
                shoppingCart.setSetmealId(shoppingCartDTO.getSetmealId());
            }
            save(shoppingCart);
        }




    }

    // 查看购物车
    @Override
    public List<ShoppingCart> seeShoppingCart() {
        Long userId = BaseContext.getCurrentId();
        return shoppingCartMapper.seeShoppingCart(userId);
    }

    // 清空购物车
    @Override
    public void removeByUserId(Long userId) {
         shoppingCartMapper.removeByUserId(userId);
    }
}
